package com.zhangsheng.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangsheng.entity.model.Product;
import com.zhangsheng.mapper.ProductMapper;
import com.zhangsheng.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
* @author hp
* @description 针对表【order】的数据库操作Service实现
* @createDate 2023-01-10 14:33:58
*/
@Service
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Override
    @DS("master")
    public boolean save(Product product) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getProductName , product.getProductName());
        Product temp = getOne(wrapper);
        if (temp != null){
            log.error("商品已存在：{}" , temp);
            throw new RuntimeException("商品已经存在");
        }
        return super.save(product);
    }

    @Override
    @DS("slave1")
    public Product selectBySlave1(Integer id) {
        Product product = getById(id);
        log.info("product:{}" , product);
        return product;
    }

    @Override
    @DS("slave2")
    public Product selectBySlave2(Integer id) {
        Product product = getById(id);
        log.info("product:{}" , product);
        return product;
    }

}




